using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {
	public game_data 	All_game_data;
	public GameObject 	Player_Object;
	public Player 		Player_script;
	public Touch_Mode 	PlayerControlObject;
		
	public Animation 	PlayerAnimation;
	public Vector3 		Player_Direction;
	
	public GameObject 	TargetObject;
	
	public GameObject 	HP_Bar;
	public GameObject 	Dash_Bar;
	public UILabel		Dash_per;
	
	private static GameManager Instance = null;
		
	public UILabel test;
	public UILabel test2;
	public int num;
	///// <summary>
	/// The frame count.
	int frameCount =0;
	float nextUpdate =0.0f;
	float fps = 0.0f;
	float updateRate = 4.0f;
	
	void Awake()
	{
		if( null == Instance )
		{
			Instance = FindObjectOfType(typeof ( GameManager ) ) as GameManager;
			if( null != Instance )
			{
				Instance.InitGameManager();
			}
		}
		if( null == Instance )
		{
			GameObject obj = new GameObject("GameManager");
			Instance = obj.AddComponent(typeof ( GameManager ) ) as GameManager;
			Instance.InitGameManager();
		}
		
	}
	
	void Update()
	{
		if( TargetObject.GetComponent<ProtectedObject>().IsGameOver().Equals(true) )
		{
			print("Game Over !! !! !! ");
			Time.timeScale = 0;
		}
		frameCount++;
		if(Time.time > nextUpdate)
		{
			nextUpdate= Time.time + 1.0f/updateRate;
			fps = (float)frameCount * updateRate;
			frameCount = 0;
			test.text = string.Format("fps:{0}",fps);
		}
		test2.text = string.Format("Object Count:{0}",num);
	}
	
	public static GameManager getInstance()
	{
		if( null == Instance )
		{
			Instance = FindObjectOfType(typeof ( GameManager ) ) as GameManager;
			if( null != Instance )
			{
				Instance.InitGameManager();
			}
		}
		if( null == Instance )
		{
			GameObject obj = new GameObject("GameManager");
			Instance = obj.AddComponent(typeof ( GameManager ) ) as GameManager;
			Instance.InitGameManager();
		}
		
		return Instance;
	}
	private void InitGameManager()
	{
		
		Player_Object = GameObject.Instantiate( Resources.Load("PlayerSet") ) as GameObject;
		Player_Object = Player_Object.transform.FindChild("Player").gameObject;
		Player_script = Player_Object.GetComponent<Player>();

		All_game_data = GameObject.Find("GameData").GetComponent<game_data>();

		TargetObject = GameObject.Instantiate( Resources.Load("ProtectedObject") ) as GameObject;
		
		DropManager.getInstance().UI_Money.text = string.Format("{0}", All_game_data.Player_Gold);
	}
}